
using System;
using System.Threading;
using lo=unoidl.com.sun.star;


namespace ConsoleApplication1
{

  // __________  implementation  ____________________________________

  /** Create and modify a spreadsheet view.
   */
  public class ViewSample : SpreadsheetDocHelper
  {

    public static void Main1(String[] args)
    {
      try
      {
        using( ViewSample aSample = new ViewSample(args) )
        {
          aSample.doSampleFunction();
        }
        Console.WriteLine("\nSamples done.");
      }
      catch( Exception ex )
      {
        Console.WriteLine("Sample caught exception! " + ex);
      }
    }

    // ________________________________________________________________

    public ViewSample(String[] args)
      : base(args)
    {
    }

    // ________________________________________________________________

    /** This sample function performs all changes on the view. */
    public void doSampleFunction()
    {
      lo.sheet.XSpreadsheetDocument xDoc = getDocument();
      lo.frame.XModel xModel =
            (lo.frame.XModel) xDoc;
      lo.frame.XController xController =
            xModel.getCurrentController();

      // --- Spreadsheet view ---
      // freeze the first column and first two rows
      lo.sheet.XViewFreezable xFreeze =
            (lo.sheet.XViewFreezable) xController;
      if( null != xFreeze )
        Console.WriteLine("got xFreeze");
      xFreeze.freezeAtPosition(1, 2);

      // --- View pane ---
      // get the cell range shown in the second pane and assign
      // a cell background to them
      lo.container.XIndexAccess xIndex =
            (lo.container.XIndexAccess) xController;
      uno.Any aPane = xIndex.getByIndex(1);
      lo.sheet.XCellRangeReferrer xRefer =
            (lo.sheet.XCellRangeReferrer) aPane.Value;
      lo.table.XCellRange xRange = xRefer.getReferredCells();
      lo.beans.XPropertySet xRangeProp =
            (lo.beans.XPropertySet) xRange;
      xRangeProp.setPropertyValue(
          "IsCellBackgroundTransparent", new uno.Any(false));
      xRangeProp.setPropertyValue(
          "CellBackColor", new uno.Any((Int32) 0xFFFFCC));

      // --- View settings ---
      // change the view to display green grid lines
      lo.beans.XPropertySet xProp =
            (lo.beans.XPropertySet) xController;
      xProp.setPropertyValue(
          "ShowGrid", new uno.Any(true));
      xProp.setPropertyValue(
          "GridColor", new uno.Any((Int32) 0x00CC00));

      // --- Range selection ---
      // let the user select a range and use it as the view's selection
      lo.sheet.XRangeSelection xRngSel =
            (lo.sheet.XRangeSelection) xController;
      ExampleRangeListener aListener = new ExampleRangeListener();
      xRngSel.addRangeSelectionListener(aListener);
      lo.beans.PropertyValue[] aArguments =
            new lo.beans.PropertyValue[2];
      aArguments[0] = new lo.beans.PropertyValue();
      aArguments[0].Name   = "Title";
      aArguments[0].Value  = new uno.Any("Please select a range");
      aArguments[1] = new lo.beans.PropertyValue();
      aArguments[1].Name   = "CloseOnMouseRelease";
      aArguments[1].Value  = new uno.Any(true);
      xRngSel.startRangeSelection(aArguments);
      Monitor.Enter(aListener);
      try
      {
        Monitor.Wait(aListener);       // wait until the selection is done
      }
      finally
      {
        Monitor.Exit(aListener);
      }
      xRngSel.removeRangeSelectionListener(aListener);
      if( aListener.aResult != null && aListener.aResult.Length != 0 )
      {
        lo.view.XSelectionSupplier xSel =
                (lo.view.XSelectionSupplier) xController;
        lo.sheet.XSpreadsheetView xView =
                (lo.sheet.XSpreadsheetView) xController;
        lo.sheet.XSpreadsheet xSheet =
                xView.getActiveSheet();
        lo.table.XCellRange xResultRange =
                xSheet.getCellRangeByName(aListener.aResult);
        xSel.select(
            new uno.Any(
                typeof(lo.table.XCellRange),
                xResultRange));
      }
    }

    // ________________________________________________________________

    //  listener to react on finished selection

    private class ExampleRangeListener
        : lo.sheet.XRangeSelectionListener
    {
      public String aResult;

      public void done(lo.sheet.RangeSelectionEvent aEvent)
      {
        aResult = aEvent.RangeDescriptor;
        Monitor.Enter(this);
        try
        {
          Monitor.Pulse(this);
        }
        finally
        {
          Monitor.Exit(this);
        }
      }

      public void aborted(
          lo.sheet.RangeSelectionEvent aEvent)
      {
        Monitor.Enter(this);
        try
        {
          Monitor.Pulse(this);
        }
        finally
        {
          Monitor.Exit(this);
        }
      }

      public void disposing(lo.lang.EventObject aObj)
      {
      }
    }

    // ________________________________________________________________

  }
}
